package gherkinedit.wizard;

import gherkinedit.templatesystem.Template;
import gherkinedit.templatesystem.XMLHandler;

import org.eclipse.jface.wizard.IWizardPage;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.Listener;

public class InsertFromTemplateWizardPage1 extends WizardPage {
	private Composite container;
	private List list;
	private InsertFromTemplateWizard wizard;
	public Template choosenTemplate = new Template("Name not initialized", "Template (not) (initialized) yet");
	private java.util.List<Template> templates = XMLHandler.getTemplates();

	public InsertFromTemplateWizardPage1(InsertFromTemplateWizard wizard) {
		super("Insert Template");
		setTitle("Select Template to insert");
		setDescription("");
		this.wizard = wizard;
	}

	@Override
	public void createControl(Composite parent) {
		container = new Composite(parent, SWT.NULL);
		GridLayout layout = new GridLayout();
		container.setLayout(layout);
		layout.numColumns = 1;

		Label label1 = new Label(container, SWT.NULL);
		label1.setText("Choose Template to insert:");

		list = new List(container, SWT.BORDER | SWT.MULTI | SWT.V_SCROLL);
		list.addListener(SWT.Selection, new Listener() {
			public void handleEvent(Event e) {
				setPageComplete(true);
				choosenTemplate = templates.get(list.getSelectionIndex());
			}
		});

		for (Template template : templates) {
			list.add(template.templateString);
		}

		GridData gd = new GridData(GridData.FILL_HORIZONTAL);
		list.setLayoutData(gd);
		// Required to avoid an error in the system
		setControl(container);
		setPageComplete(false);

	}

	public String getSelectedTemplate() {
		return list.getSelection()[0];
	}
	
	@Override
	public IWizardPage getNextPage() {
		IWizardPage nextPage = super.getNextPage();
		wizard.drawPage2();
		return nextPage;
	}	
}
